package com.qffx.moviebase.view.classify;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.qffx.moviebase.R;

import java.util.ArrayList;
import java.util.List;

public class ClasifySearchActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private FrameLayout frame;
    private RadioGroup rg;
    private ImageView iv;

    private ArticleFragment articleFragment = new ArticleFragment();
    private MovieFragment movieFragment = new MovieFragment();
    private FragmentTransaction transaction;
    private List<Fragment> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clasify_search);
        init();

    }

    private void init() {
        frame = (FrameLayout) findViewById(R.id.class_search_frame);
        rg = (RadioGroup) findViewById(R.id.search_rg);
        iv= (ImageView) findViewById(R.id.search_iv);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        rg.setOnCheckedChangeListener(this);
        list.add(articleFragment);
        list.add(movieFragment);
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.class_search_frame,list.get(0));
        transaction.commit();


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.search_bt1:
                showFragment(0);
                break;
            case R.id.search_bt2:
                showFragment(1);
                break;


        }

    }

    //展示fragment的方法
    private void showFragment(int position) {
        transaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < list.size(); i++) {
            if (i == position) {
                if (!list.get(i).isAdded()) {
                    transaction.add(R.id.class_search_frame, list.get(i));
                }
                transaction.show(list.get(i));
            } else {
                transaction.hide(list.get(i));
            }
        }
        transaction.commit();
    }


}
